import os
import pandas as pd
import re
import numpy as np
from utils import *
import sys

read_dir = r"D:\data_cmp\airfoil_dataset"
save_dir = r"D:\data_cmp\airfoil_csv"
move27_dir = r"D:\data_cmp\airfoil_csv_27"
move205_dir = r"D:\data_cmp\airfoil_csv_205"

if __name__ == "__main__":
    # step 1: rename
    for file_name in os.listdir(read_dir):
        if ".DAT" in file_name:
            file_rename(os.path.join(read_dir, file_name))

    # test
    for file_name in os.listdir(read_dir):
        if ".DAT" in file_name:
            print(file_name + r"has '.DAT'")

    # step 2: count number && .dat to .csv
    for file_name in os.listdir(read_dir):
        save_name = file_name[:-3]+"csv"
        prc_svcsv(os.path.join(read_dir, file_name),
                  os.path.join(save_dir, save_name))
    #
    # sys.exit()

    # count
    count = []
    count_less_27 = 0
    count_more_205 = 0
    for file_name in os.listdir(save_dir):
        tmp_count = csv_count(os.path.join(save_dir, file_name))
        count.append(tmp_count)
        if tmp_count <= 27:
            count_less_27 += 1
            print(file_name, tmp_count)
            # move(os.path.join(save_dir, file_name), os.path.join(move27_dir, file_name))
        if tmp_count >= 205:
            count_more_205 += 1
            # move(os.path.join(save_dir, file_name), os.path.join(move205_dir, file_name))

    print("mean: ", np.mean(count))
    print("std: ", np.std(count))
    print("mean-3*sigma: ", np.mean(count)-np.std(count)*3)

    count = np.asarray(count)
    print("there are {} samples whose number less than 27".format(count_less_27))
    print("there are {} samples whose number more than 79".format(count_more_205))
    print("1%:", np.percentile(count, 1))
    print("3%:", np.percentile(count, 3))
    print("5%:", np.percentile(count, 5))
    print("8%:", np.percentile(count, 8))
    print("10%:", np.percentile(count, 10))
    print("92%:", np.percentile(count, 92))
    print("95%:", np.percentile(count, 95))
    print("98%:", np.percentile(count, 98))
    print("99%:", np.percentile(count, 99))

    print("mean:", count.mean())

    # naca64a010.dat和lrn1007.dat特殊处理（科学计数法）

    # step 3: find the number out of range and modify to normal range
    for file_name in os.listdir(save_dir):
        csv_out_range(os.path.join(save_dir, file_name))

    # step 4: draw the dot
    # for file_name in os.listdir(move27_dir):
    #     df = pd.read_csv(os.path.join(move27_dir, file_name))
    #     draw(df['x'], df['y'])

    # step 5: Cubic spline interpolation
    # for file_name in os.listdir(move27_dir):
    #     df = pd.read_csv(os.path.join(move27_dir, file_name))
    #     print(df.shape)
    #     X = np.asarray(df['x'])
    #     Y = np.asarray(df['y'])
    #     mid = 0
    #     while X[mid+1] < X[mid]:
    #         mid += 1
    #     csi(X[0:mid+1][::-1], Y[0:mid+1][::-1], X[mid::], Y[mid::], file_name) # 将曲线分为上下两部分，且样本点的横坐标应是递增的


